package com.online_video.filter;

import com.online_video.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author zhenghouwen
 * @create 2022-03-17 22:44
 */
//@WebFilter("/client/collection.jsp")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        HttpServletRequest req = (HttpServletRequest) request;
        //获取到 session
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute("user");
        if (user == null){
            response.getWriter().write("您还没有登录");
        }else {
            chain.doFilter(request,response);
        }

    }

    @Override
    public void destroy() {

    }
}
